8201db
@@ -118,6 +118,12 @@
public class HTTPUpgradeService implements Service<HTTPUpgradeService> {
                     public boolean handleUpgrade(HttpServerExchange exchange) throws IOException {
 
                         if (super.handleUpgrade(exchange)) {
+                            // If ActiveMQ remoting service is stopped (eg during shutdown), refuse
+                            // the handshake so that the ActiveMQ client will detect the connection has failed
+                            RemotingService remotingService = activeMQServer.getRemotingService();
+                            if (!remotingService.isStarted() || remotingService.isPaused()) {
+                                return false;
+                            }
                             final String endpoint = exchange.getRequestHeaders().getFirst(getHttpUpgradeEndpointKey());
                             if (endpoint == null) {
                                 return true;
@@ -149,15 +155,19 @@
public class HTTPUpgradeService implements Service<HTTPUpgradeService> {
             public void handleEvent(final StreamConnection connection) {
                 MessagingLogger.ROOT_LOGGER.debugf("Switching to %s protocol for %s http-acceptor", protocolName, acceptorName);
                 RemotingService remotingService = activemqServer.getRemotingService();
-                if (!remotingService.isStarted()) {
+                if (!remotingService.isStarted() || remotingService.isPaused()) {
                     // ActiveMQ no longer accepts connection
                     IoUtils.safeClose(connection);
                     return;
                 }
                 NettyAcceptor acceptor = (NettyAcceptor) remotingService.getAcceptor(acceptorName);
                 SocketChannel channel = new WrappingXnioSocketChannel(connection);
-                acceptor.transfer(channel);
-                connection.getSourceChannel().resumeReads();
+                try {
+                    acceptor.transfer(channel);
+                    connection.getSourceChannel().resumeReads();
+                } catch (IllegalStateException e) {
+                    IoUtils.safeClose(connection);
+                }
             }
         };
     }
